from flask import render_template,session,request,jsonify
from info.models import User,News,Category
from . import index_blueprint
from info import constants
from info.response_code import RET
from info.utils.commons import get_click_list

@index_blueprint.route('/')
def index():
    '''查询数据并显示'''
    # 用户登录状态
    user_id=session.get('user_id')
    if user_id is None:
        user=None
    else:
        user=User.query.get(user_id).to_login_dict()

    # 分类信息
    # 查询所有分类
    category_list1=Category.query.all()
    # 转字典
    category_list2=[category.to_dict() for category in category_list1]

    # 点击排行
    news_list2 = get_click_list()

    # 新闻列表
    data={
        'user':user,
        'news_list':news_list2,
        'category_list':category_list2
    }

    return render_template('news/index.html',data=data)

@index_blueprint.route('/newslist')
def newlist():
    # 接收：分类编号，页码，页大小
    page = request.args.get('page')
    cid = request.args.get('cid')
    per_page = request.args.get('per_page')

    # 验证
    if not all([page,cid,per_page]):
        return jsonify(errno=RET.NODATA,errmsg='数据不完整')

    # 3.处理：查询，过滤，分页
    pagination = News.query
    # 如果分类编号大于0 表示指定某个分类，则拼接filter_by()语句
    if int(cid) > 0:
        pagination = pagination.filter_by(category_id=cid)
    # order_by
    pagination = pagination.order_by(News.id.desc()).paginte(int(page),int(per_page),False)
    # 当前页数据
    news_list1 = pagination.items
    # 总页数
    total_page = pagination.pages

    # 响应
    news_list2 = [news.to_index_dict() for news in news_list1]

    data = {
        'totalPage' : total_page,
        'newsList' : news_list2
    }
    return jsonify(data)